package com.orangelife.mobile.common.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.adapter.SimpleAdapter;

public class YfRefreshBaseFragment extends OrangeLifeBaseFragment {
	protected boolean mLoadingLock = false;
	private View homePageView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		    homePageView = inflater.inflate(R.layout.include_sample,null);
			return homePageView;
	}

	protected void initSwipeLayout(View homePageView) {
		final SwipeRefreshLayout mSwipeLayout = (SwipeRefreshLayout)homePageView.findViewById(R.id.swipe_container);
		mSwipeLayout.setColorSchemeResources(
				android.R.color.holo_blue_light,
				android.R.color.holo_green_light,
				android.R.color.holo_orange_light,
				android.R.color.holo_red_light);
		mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						mSwipeLayout.setRefreshing(false);
						refresh();
					}
				}, 5000);
			}
		});
	}

	public void loadMore(SimpleAdapter mAdapter,int mTotalDataCount) {
		if (mLoadingLock) {
			return;
		}
		if (mAdapter.getData().size() < mTotalDataCount && mAdapter.getData().size() > 0) {
			// has more
			mLoadingLock = true;
			if (!mAdapter.getFooters().contains("loading...")) {
				mAdapter.addFooter("loading...");
			}
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					load();
				}
			}, 3000);
		} else {
			// no more
			if (mAdapter.getFooters().contains("loading...")) {
				mAdapter.removeFooter("loading...");
			}
			mAdapter.addFooter("无更多内容");
		}
	}

	protected  void refresh(){}
	protected  void load(){}
}
